<?php
/**
 * Concrete class for handling Smarty view scripts.
 *
 * @category   AkCom
 * @package    AkCom_View
 * @copyright  Copyright (c) 2006 Rob Allen (http://www.akrabat.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
require_once 'Twig/Autoloader.php';

class Twig_View implements Zend_View_Interface
{
    protected $_engine = null;
	protected $_templateparas = array();
	protected $scriptpath = array();
	
    function __construct($scriptpath)
    {
	    /**
		* Twig Library
		* Add Extension: Escaper,Sanbox,Filter 
		*/		
		Twig_Autoloader::register();
		$this->setScriptPath($scriptpath);
		$loader = new Twig_Loader_Filesystem($scriptpath);
		$this->_engine = new Twig_Environment($loader, array(
		  //'cache' => TWIG_PATH .'/compilation_cache'
		));
		$escaper = new Twig_Extension_Escaper(true);
		$this->_engine->addExtension($escaper);
    }

    public function getEngine()
    {
        return $this->_engine;
    }

    public function setScriptPath($path)
    {
    	array_push($this->scriptpath,$path);
    }

    public function __set($key, $val)
    {
    	if(is_null($this->$_templateparas))
    	{
    		$this->$_templateparas = array();
    	}
       $this->$_templateparas[$key]=$val;
    }

    public function __get($key)
    {
        return $this->$_templateparas[$key];
    }

    public function __isset($key)
    {
        $var = $this->_engine->get_template_vars($key);
        return !is_null($var); 
    }

    public function __unset($key)
    {
        if (is_array($key))
            foreach ($key as $curr_var)
                unset($this->_templateparas[$curr_var]);
        else
            unset($this->_templateparas[$key]);
    }

    public function assign($spec, $value = null)
    {
     	if (is_array($this->_templateparas)){
//            foreach ($this->_templateparas as $spec => $value) {
//                if ($spec != '') {
//                    $this->_templateparas[$spec] = $value;
//                }
//            }
//        } else {
            if ($this->_templateparas != '')
                $this->_templateparas[$spec] = $value;
        }
    }

    public function clearVars()
    {
        
    }

    public function render($name)
    {
    	$template = $this->_engine->loadTemplate($name); 		
        return $template->display($this->_templateparas);
    }


    public function setCompilePath($dir)
    {    	
        //$this->_engine->compile_dir = $dir;
    }
    
    public function setCachePath($dir)
    {
        $this->_engine->setCache($dir);
    }
    public function getScriptPaths()
    {
    	return $this->scriptpath;
    	
    }
    public function setBasePath($path, $classPrefix = 'Zend_View')
    {
    	//$this->scriptpath = $path;    	
    }
    
    public function addBasePath($path, $classPrefix = 'Zend_View')
    {
 		//array_push($this->scriptpath,$path);   	    	
    }
}